/*
Handles many features such as switching between 2d and 3d.
*/
#include <string>

int keyDown[256]; //256
int sKeyDown[256]; //http://www.opengl.org/resources/libraries/glut/spec3/node54.html

//void mouse(int
void keyboardSpecial(int key, int x, int y){
    sKeyDown[key] = 1;
}
void keyboardUpSpecial(int key, int x, int y){
    sKeyDown[key] = 0;
}
void keyboard(unsigned char key, int x, int y){
    keyDown[key] = 1;
}
void keyboardUp(unsigned char key, int x, int y){
    keyDown[key] = 0;
}
void glEnable2D(){
	GLint iViewport[4];

	// Get a copy of the viewport
	glGetIntegerv( GL_VIEWPORT, iViewport );

	// Save a copy of the projection matrix so that we can restore it
	// when it's time to do 3D rendering again.
	glMatrixMode( GL_PROJECTION );
	glPushMatrix();
	glLoadIdentity();

	// Set up the orthographic projection
	glOrtho( iViewport[0], iViewport[0]+iViewport[2],
			 iViewport[1]+iViewport[3], iViewport[1], -1, 1 );
	glMatrixMode( GL_MODELVIEW );
	glPushMatrix();
	glLoadIdentity();

	// Make sure depth testing and lighting are disabled for 2D rendering until
	// we are finished rendering in 2D
	glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_LIGHTING_BIT );
	glDisable( GL_DEPTH_TEST );
	glDisable( GL_LIGHTING );
}
void glDisable2D()
{
	glPopAttrib();
	glMatrixMode( GL_PROJECTION );
	glPopMatrix();
	glMatrixMode( GL_MODELVIEW );
	glPopMatrix();
}
/*
void glDisable2D()
{
	glPopAttrib();
	glMatrixMode( GL_PROJECTION );
	glPopMatrix();
	glMatrixMode( GL_MODELVIEW );
	glPopMatrix();
}

static void resize(int width, int height){ //change 800 to width and 600 to height
    GLdouble ASPECTRATIO = (GLdouble)width/(GLdouble)height;
    glViewport(0, 0, width, height);            
    glMatrixMode(GL_PROJECTION);                  
    glLoadIdentity();				

    gluPerspective(45.0f,(GLfloat)ASPECTRATIO,0.1f,200.0f);  

    glMatrixMode(GL_MODELVIEW);                  

    glClearAccum(0.0, 0.0, 0.0, 1.0);
    glClear(GL_ACCUM_BUFFER_BIT);
}
*/
static void resize(int width, int height){ //change 800 to width and 600 to height
    GLdouble ASPECTRATIO = (GLdouble)width/(GLdouble)height;
    glViewport(0, 0, width, height);              /* Set the viewport */
    glMatrixMode(GL_PROJECTION);                  /* Select the projection matrix */
    glLoadIdentity();				/* Reset The Projection Matrix */

    gluPerspective(45.0f,(GLfloat)ASPECTRATIO,0.1f,200.0f);  /* Calculate The Aspect Ratio Of The Window FIRST 1*/

    glMatrixMode(GL_MODELVIEW);                   /* Switch back to the modelview matrix */

    glClearAccum(0.0, 0.0, 0.0, 1.0);
    glClear(GL_ACCUM_BUFFER_BIT);

}



int frame=0,time2,timebase=0;
int oldTime=GLUT_ELAPSED_TIME;
void updateWorld();
void mainLoop() {
    if(glutGet(GLUT_ELAPSED_TIME)-oldTime > 1000/69){
        oldTime = glutGet(GLUT_ELAPSED_TIME);

        frame++;
        time2=glutGet(GLUT_ELAPSED_TIME);
        if (time2 - timebase > 1000) {
            //std::cout << "FPS: " <<frame*1000.0/(time2-timebase) << "\n";
            timebase = time2;
            frame = 0;
        }

        updateWorld();
    }
}

void update(){
    glutPostRedisplay();
}
